package com.papers.juan.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.annotation.Log;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.core.validate.QueryGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.juan.domain.vo.TJuanReposRelatedVo;
import com.papers.juan.domain.bo.TJuanReposRelatedBo;
import com.papers.juan.service.ITJuanReposRelatedService;
import com.papers.common.core.page.TableDataInfo;

/**
 * 题库题目关联
 *
 * @author papers
 * @date 2022-10-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/juan/juanReposRelated")
public class TJuanReposRelatedController extends BaseController {

    private final ITJuanReposRelatedService iTJuanReposRelatedService;

    /**
     * 查询题库题目关联列表
     */
    @SaCheckPermission("juan:juanReposRelated:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanReposRelatedVo> list(TJuanReposRelatedBo bo, PageQuery pageQuery) {
        return iTJuanReposRelatedService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题库题目关联列表
     */
    @SaCheckPermission("juan:juanReposRelated:export")
    @Log(title = "题库题目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanReposRelatedBo bo, HttpServletResponse response) {
        List<TJuanReposRelatedVo> list = iTJuanReposRelatedService.queryList(bo);
        ExcelUtil.exportExcel(list, "题库题目关联", TJuanReposRelatedVo.class, response);
    }

    /**
     * 获取题库题目关联详细信息
     *
     * @param reposId 主键
     */
    @SaCheckPermission("juan:juanReposRelated:query")
    @GetMapping("/{reposId}")
    public R<TJuanReposRelatedVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String reposId) {
        return R.ok(iTJuanReposRelatedService.queryById(reposId));
    }

    /**
     * 新增题库题目关联
     */
    @SaCheckPermission("juan:juanReposRelated:add")
    @Log(title = "题库题目关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanReposRelatedBo bo) {
        return toAjax(iTJuanReposRelatedService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改题库题目关联
     */
    @SaCheckPermission("juan:juanReposRelated:edit")
    @Log(title = "题库题目关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanReposRelatedBo bo) {
        return toAjax(iTJuanReposRelatedService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除题库题目关联
     *
     * @param reposIds 主键串
     */
    @SaCheckPermission("juan:juanReposRelated:remove")
    @Log(title = "题库题目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reposIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] reposIds) {
        return toAjax(iTJuanReposRelatedService.deleteWithValidByIds(Arrays.asList(reposIds), true) ? 1 : 0);
    }
}
